﻿using LightCAD.Core;
using LightCAD.Runtime;
using SkiaSharp.Views.Desktop;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LightCAD.UI
{
    public partial class ModelSpaceControl : UserControl
    {
        public ModelSpaceControl()
        {
            InitializeComponent();
            this.Load += ModelSpaceControl_Load;
            this.VisibleChanged += ModelSpaceControl_VisibleChanged;
        }

        private void ModelSpaceControl_Load(object? sender, EventArgs e)
        {
        }

        private void ModelSpaceControl_VisibleChanged(object? sender, EventArgs e)
        {
            foreach(var item in Viewports)
            {
                item.Visible = this.Visible;
            }
        }


        public List<ViewportControl> Viewports { get; } = new List<ViewportControl>();
        public void SetLayout(LcLayout layout)
        {
            ClearLayout();

            if (layout.Type == LayoutType.One)
            {
                var vportCtrl = new ViewportControl();
                vportCtrl.Tag = layout.Viewports[0].Id;
                Viewports.Add( vportCtrl);
                vportCtrl.Dock = DockStyle.Fill;
                this.Controls.Add(vportCtrl);
            }
            else if (layout.Type == LayoutType.HorzTwo || layout.Type == LayoutType.VertTwo)
            {
                var splitContainer = new SplitContainer();
                splitContainer.Dock = DockStyle.Fill;
                //容器方向和视口的布局方向相反的
                splitContainer.Orientation = layout.Type == LayoutType.HorzTwo ? Orientation.Vertical : Orientation.Horizontal;
                this.Controls.Add(splitContainer);
                splitContainer.SplitterDistance = (this.Width - splitContainer.SplitterWidth) / 2;

                //留边框用于显示哪个视口处于激活状态
                splitContainer.Panel1.Padding = new Padding(2);
                splitContainer.Panel2.Padding = new Padding(2);


                var vport1 = new ViewportControl();
                vport1.Tag = layout.Viewports[0].Id;
                Viewports.Add( vport1);
                vport1.Dock = DockStyle.Fill;
                splitContainer.Panel1.Controls.Add(vport1);

                var vport2= new ViewportControl();
                vport2.Tag = layout.Viewports[1].Id;
                Viewports.Add( vport2);
                vport2.Dock = DockStyle.Fill;
                splitContainer.Panel2.Controls.Add(vport2);
            }
            else if (layout.Type == LayoutType.Four)
            {
                var splitContainer = new SplitContainer();
                splitContainer.Dock = DockStyle.Fill;
                splitContainer.Orientation = Orientation.Horizontal;
                this.Controls.Add(splitContainer);

                var scLeft = new SplitContainer();
                scLeft.Dock = DockStyle.Fill;
                scLeft.Orientation = Orientation.Vertical;
                splitContainer.Panel1.Controls.Add(scLeft);

                scLeft.Panel1.Padding = new Padding(2);
                scLeft.Panel2.Padding = new Padding(2);


                var scRight = new SplitContainer();
                scRight.Dock = DockStyle.Fill;
                scRight.Orientation = Orientation.Vertical;
                splitContainer.Panel2.Controls.Add(scRight);

                scRight.Panel1.Padding = new Padding(2);
                scRight.Panel2.Padding = new Padding(2);


                var vport1 = new ViewportControl();
                vport1.Tag = layout.Viewports[0].Id;
                Viewports.Add( vport1);
                vport1.Dock = DockStyle.Fill;
                scLeft.Panel1.Controls.Add(vport1);
                
                var vport2 = new ViewportControl();
                vport2.Tag = layout.Viewports[1].Id;
                Viewports.Add( vport2);
                vport2.Dock = DockStyle.Fill;
                scLeft.Panel2.Controls.Add(vport2);

                var vport3 = new ViewportControl();
                vport3.Dock = DockStyle.Fill;
                vport3.Tag = layout.Viewports[2].Id;
                Viewports.Add(vport3);
                scRight.Panel1.Controls.Add(vport3);

                var vport4 = new ViewportControl();
                vport4.Tag = layout.Viewports[3].Id;
                Viewports.Add( vport4);
                vport4.Dock = DockStyle.Fill;
                scRight.Panel2.Controls.Add(vport4);
            }



        }
        internal void ClearLayout()
        {
            this.Controls.Clear();
            foreach (var item in Viewports)
            {
                item.Dispose();
            }
            Viewports.Clear();
        }
     
    }
}
